Python

How to integrate NicEdit rich text editor for the description field in a Django Python?

How to integrate NicEdit rich text editor for the description field in a Django Python?, someone asked me to explain?

To integrate NicEdit in django Admin:

Download NicEdit from official website or register NicEdit script into project. Add NicEdit JavaScript file into project's static folder. Create an folder named "js" and place the NicEdit JavaScript file.

Django Admin Configuration:

form.py:

from .models import Product
from django import forms
class ProductForm(forms.ModelForm):
    class Meta:
        model = Product  # Specify your model
        fields = '__all__'  # Or list the fields you want to include
    # Add a widget to the description field
    description = forms.CharField(widget=forms.Textarea(attrs={'class': 'nic-edit','cols': 80}))

admin.py:

from django.contrib import admin
from .models import *
from .form import ProductForm
class ProductAdmin(admin.ModelAdmin):
  list_display = ('name', 'description')
  list_per_page=10
  search_fields=['name','category__name']
  form = ProductForm
  class Media:
        js = ('/static/js/nicEdit.js', '/static/js/script.js')
admin.site.register(Product, ProductAdmin)
script.js:
document.addEventListener("DOMContentLoaded", function() {
    var textAreas = document.getElementsByClassName("nic-edit");
    for (var i = 0; i < textAreas.length; i++) {
        new nicEditor({ fullPanel: true }).panelInstance(textAreas[i]);
    }
});
 rich text editor for description field in django admin

VIDEO GUIDE:

Post your comments / questions